package com.ruoyi.web.controller.User;

import java.util.Arrays;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.School.domain.School;
import com.ruoyi.School.service.ISchoolService;
import com.ruoyi.User.domain.User;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.UserAddress.domain.UserAddress;
import com.ruoyi.UserAddress.service.IUserAddressService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 用户 信息操作处理
 *
 * @author ruoyi
 * @date 2019-07-25
 */
@Controller
@RequestMapping("/User/user")
public class UserController extends BaseController
{
    private String prefix = "User/user";

	@Autowired
	private IUserService userService;

	@Autowired
	private ISchoolService schoolService;

	@Autowired
	private IUserAddressService userAddressService;

	@RequiresPermissions("User:user:view")
	@GetMapping()
	public String user(ModelMap mmap)
	{
		School example = new School();
		List<School> schoolList = schoolService.selectSchoolList(example);
		mmap.put("schoolList", schoolList);
		return prefix + "/user";
	}

	/**
	 * 查询用户列表
	 */
	@RequiresPermissions("User:user:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(User user)
	{
		startPage();
        List<User> list = userService.selectUserList(user);
		return getDataTable(list);
	}

	/**
	 * 查询用户地址中间列表
	 */
	@RequiresPermissions("UserAddress:userAddress:list")
	@PostMapping("/addressList")
	@ResponseBody
	public TableDataInfo addressList(UserAddress userAddress)
	{
		startPage();
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
		return getDataTable(list);
	}

	/**
	 * 获取用户地址
	 */
	@GetMapping("/getAddress/{id}")
	public String getAddress(@PathVariable("id") Integer id, ModelMap mmap)
	{
		mmap.put("userId", id);
	    return prefix + "/userAddress";
	}

	/**
	 * 获取用户金额流向
	 */
	@GetMapping("/getMoney/{id}")
	public String getMoney(@PathVariable("id") Integer id, ModelMap mmap)
	{
		mmap.put("userId", id);
		return prefix + "/money";
	}

	/**
	 * 获取用户红包
	 */
	@GetMapping("/getPacket/{id}")
	public String getPacket(@PathVariable("id") Integer id, ModelMap mmap)
	{
		mmap.put("userId", id);
	    return prefix + "/hbUser";
	}

	/**
	 * 获取用户红包
	 */
	@GetMapping("/getSign/{id}")
	public String getSign(@PathVariable("id") Integer id, ModelMap mmap)
	{
		mmap.put("userId", id);
	    return prefix + "/sign";
	}


	/**
	 * 导出用户列表
	 */
	@RequiresPermissions("User:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(User user)
    {
    	List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "user");
    }

	/**
	 * 新增用户
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 新增保存用户
	 */
	@RequiresPermissions("User:user:add")
	@Log(title = "用户", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(User user)
	{
		return toAjax(userService.insertUser(user));
	}

	/**
	 * 修改用户
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		User user = userService.selectUserById(id);
		School school = new School();
		List<School> schoolList = schoolService.selectSchoolList(school);
		if (schoolList.size() > 0) {
			for (School example : schoolList) {
				if (setFlag(user, example)) {
					example.setFlag(true);
				}
			}
		}
		mmap.put("user", user);
		mmap.put("schoolList", schoolList);
		User example=new User();
		example.setChiefPermission(1);
		List<User> chiefList=userService.selectUserList(example);
		mmap.put("chiefList", chiefList);
		return prefix + "/edit";
	}

	/**
	 * 修改保存用户
	 */
	@RequiresPermissions("User:user:edit")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(User user)
	{
		if(user.getService()==null){
			user.setService("");
		}
		return toAjax(userService.updateUser(user));
	}

	/**
	 * 删除用户
	 */
	@RequiresPermissions("User:user:remove")
	@Log(title = "用户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(userService.deleteUserByIds(ids));
	}

	private static Boolean setFlag(User user, School school) {
		String schools = user.getSchools();
		String schoolName = school.getSchoolName();
		if(!StringUtils.isEmpty(schools)){
			String[] trueSchool = schools.split(",");
			if (Arrays.asList(trueSchool).contains(schoolName)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

}
